package com.taotao.search.service;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.search.dao.SearchDao;
import com.taotao.search.pojo.SearchResult;
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SearchDao searchDao;
	/**
	 * 根据前台传入的(搜索词汇,当前页码,每页记录数)作参数返回结果
	 * @param queryString
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 * @author: JSJ
	 */
	@Override
	public SearchResult search(String queryString, int page, int rows)
			throws Exception {
		SolrQuery query=new SolrQuery();
		//设置搜索关键字
		query.setQuery(queryString);
		//设置分页条件
		if(page<=0){
			page=1;
		}
		query.setStart((page-1)*rows);
		if(rows<=0){
			rows=20;
		}
		query.setRows(rows);
		//设置默认查询域
		query.set("df","item_title");
		//设置高亮
		query.setHighlight(true);
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<font class=\"skcolor_ljg\">");
		query.setHighlightSimplePost("</font>");
		//执行查询
		SearchResult result = searchDao.search(query);
		long total=result.getRecordCount();
		int pageCount=(int) (total / rows);
		if (total % rows >0) {
			pageCount++;
		}
		result.setPageCount(pageCount);
		result.setCurPage(page);
		return result;
	}

}
